Next | Prev | Up | Top | Contents | Index

General-Purpose Allocation

There are two groups of general-purpose functions used to allocate and release memory.

The functions you can use to dynamically allocate kernel virtual memory are summarized in Table 9-3.

Functions for Kernel Virtual Memory
Function NameHeader FilesCan Sleep?Purpose
kmem_alloc(D3) kmem.h & types.hYAllocate space from kernel free memory.
kmem_free(D3) kmem.h & types.hNFree previously allocated kernel memory.
kmem_zalloc(D3) kmem.h & types.hYAllocate and clear space from kernel free memory.
kern_calloc(D3) systm.h & types.hYAllocate space from kernel memory and clear it.
kern_free(D3) systm.h & types.hNFree kernel memory space.
kern_malloc(D3) systm.h & types.hYAllocate kernel virtual memory.

The most important of these functions is kmem_alloc(). You use it to allocate blocks of virtual memory at any time. It offers these important options, controlled by a flag argument:

The kmem_zalloc() function takes the same options, but offers the additional service of zero-filling the allocated memory.

Calls to the "kern" group of functions should be replaced as follows:

kern_malloc(n)Change to kmem_alloc(n,KM_SLEEP).
kern_calloc(n,s)Change to kmem_zalloc(n*s,KM_SLEEP)
kern_free(p)Change to kmem_free(p)


Next | Prev | Up | Top | Contents | Index